//
//  TravelsAuthor_LoveView.m
//  艾听歌
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TravelsAuthor_LoveView.h"

#import "TravelsAuthor_LoveCollectionViewCell.h"


#import "ModuleFirst_UserLoveModel.h"


static NSString *const collectionViewCell = @"reuse";

@interface TravelsAuthor_LoveView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *loveCollectionView;

@property (nonatomic, strong) NSMutableArray *allLoveArray;


@end

@implementation TravelsAuthor_LoveView

- (instancetype)initWithFrame:(CGRect)frame {
 
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createSubViews];
    }
    
    return self;
}

- (void)createSubViews{
    
    //flowLayout布局
    TravelsAuthor_LoveCollectionFlowLayout *flowLayout = [[TravelsAuthor_LoveCollectionFlowLayout alloc]init];
    
//    flowLayout.itemSize = CGSizeMake((self.frame.size.width -30) / 2, (self.frame.size.height - 40) / 3);
//    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //成为代理人
    flowLayout.ytDelegate = self;
    
    //每个item的大小
    flowLayout.itemSize = CGSizeMake((self.frame.size.width -25) / 2, (self.frame.size.height - 30) / 3);
    //边界值
    flowLayout.edgeInsets = UIEdgeInsetsMake(0, 10, 10, 10);
    //列数
    flowLayout.columCounts = 2;
    
    //列边距
    flowLayout.columSpace = 5;
    
    //行边距
    flowLayout.rowSpace = 5;

    //设置滑动方向
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

    
    
    self.loveCollectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
    self.loveCollectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.loveCollectionView];
    
    //成为代理人
    self.loveCollectionView.dataSource = self;
    self.loveCollectionView.delegate = self;
    
    //collectionViewcell注册
    [self.loveCollectionView registerClass:[TravelsAuthor_LoveCollectionViewCell class] forCellWithReuseIdentifier:collectionViewCell];
    
    

    
    self.isUpLoading = NO;
    self.allLoveArray = [NSMutableArray array];
    
    // 2.集成上拉下拉功能
    [self addHeader];
    [self addFooter];
    
    
}


#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    
    __block TravelsAuthor_LoveView *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    self.loveCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        vc.nextPage = 1;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getAFNetworkingLoveData:vc.nextPage];//重新请求数据

        
    }];
    
    
    
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.loveCollectionView.mj_header beginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block TravelsAuthor_LoveView *vc = self;
    self.loveCollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        vc.nextPage ++;
             vc.isUpLoading = YES;//标记为上拉操作
        [vc getAFNetworkingLoveData:vc.nextPage];//请求数据
        
    }];
    }



//(喜欢)网络请求数据
- (void)getAFNetworkingLoveData:(NSInteger)nextCursor{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    //创建一个管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    [manager.securityPolicy setValidatesDomainName:NO];
    //识别各种类型的网址
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    NSString *idd = self.UIModel.idd.stringValue;
    NSString *string = [NSString stringWithFormat:@"https://chanyouji.com/api/users/likes/%@.json?per_page=15&page=%ld", idd, nextCursor];
    [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [netWorkManager stopMonitoring];
        
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.allLoveArray removeAllObjects];
        }
        
        //        NSLog(@"%@", responseObject);
        for (NSDictionary *smallDic in responseObject) {
            
            ModuleFirst_UserLoveModel *MF_UserLoveModel = [[ModuleFirst_UserLoveModel alloc]init];
            [MF_UserLoveModel setValuesForKeysWithDictionary:smallDic];
            
            [self.allLoveArray addObject:MF_UserLoveModel];
        }
        
        
        
        [self.loveCollectionView.mj_footer endRefreshing];
        [self.loveCollectionView.mj_header endRefreshing];
        
        [self.loveCollectionView reloadData];
        
        self.backArrToVC(self.allLoveArray);

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error====%@",error);

    }];
     

}






//****必须做到
//显示每个cell的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TravelsAuthor_LoveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCell forIndexPath:indexPath];
    
//    cell.backgroundColor = [UIColor redColor];
    cell.cellModel = self.allLoveArray[indexPath.row];
    
    return  cell;
}

//每个分区有点多少个cell

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
//    NSLog(@"count===%ld",self.countArray.count);
    return self.allLoveArray.count;
}


//设置 item 的高度
-(CGFloat)ytFlowLayout:(TravelsAuthor_LoveCollectionFlowLayout *)flowLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath itemWidth:(CGFloat)width{
    
    
    //    DataModel *data = self.myArray[indexPath.row];
    //
    //    //等比例缩放(!!!!!)
    //    //图片原始宽 / item宽 = 原始高 / item高
    //    CGFloat height = [data.height floatValue]  * width / [data.width floatValue];
    //
    //    return height;
    
    ModuleFirst_UserLoveModel *smallModel = self.allLoveArray[indexPath.row];
    
    CGFloat height = [smallModel.height floatValue] * width / [smallModel.width floatValue];
    
    return height;
    
}



-(void)layoutSubviews{
    
//    NSLog(@"allArray ==%ld",self.allLoveArray.count);
//    NSLog(@"countArray ==%ld",self.countArray.count);

}




@end
